package com.a2020111222.test.proxy;

import com.a2020111222.test.aspect.userAspect;
import com.a2020111222.test.dao.userDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Administrator
 * @version 1.0
 * @description: JDK代理类
 * @date 2022/11/6 17:37
 */
public class userJDKProxy implements InvocationHandler {
//    声明目标类接口
    private userDao userDao;

    public Object createProxy(userDao userDao){
        this.userDao = userDao;
//        1.类加载器
        ClassLoader classLoader = userJDKProxy.class.getClassLoader();
//        2.被代理对象实现所有接口
        Class[] classes = userDao.getClass().getInterfaces();
//        3.使用代理进行增强，返回被代理对象
        return Proxy.newProxyInstance(classLoader,classes,this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        切面对象
        userAspect userAspect = new userAspect();
//        前增强
        userAspect.checkPermissions();
//        目标类调用方法，传入参数
        Object obj = method.invoke(userDao ,args);
//        后增强
        userAspect.log();
        return obj;
    }
}
